package com.linwen.excise.leetcode;

import com.sun.xml.internal.ws.api.pipe.NextAction;

/**
 * @ClassName _155最小栈
 * @Description TODO
 * @Author zero
 * @DATE 2024/4/17 11:34 PM
 * @Version 1.0
 */
public class _155最小栈 {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.getMin());
    }

}

class MinStack {
    MyStackNode head;

    public MinStack() {
        head = new MyStackNode();
    }

    public void push(int val) {
        MyStackNode node = new MyStackNode(val,head.next);
        node.min = Math.min(val,head.next==null?Integer.MAX_VALUE:head.next.min);
        head.next = node;
    }

    public void pop() {
        MyStackNode next = head.next;
        head.next = next.next;

    }

    public int top() {
        return head.next.value;

    }

    public int getMin() {
        return head.next.min;
    }
}
class MyStackNode{
    int value,min = Integer.MAX_VALUE;
    MyStackNode next;
    MyStackNode(){}
    MyStackNode(int value,MyStackNode next){
        this.value = value;
        this.next = next;
    }
}